package ru.eas.tetris;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import ru.eas.tetris.tetromino.RotatableTetromino;

/**
 * @author aesipov
 */
public class Figure
{
    private RotatableTetromino tetromino;
    private int x0 = 0;
    private int y0 = 0;
    private Color              color;

    public Figure(RotatableTetromino tetromino,
                  Color color)
    {
        this.tetromino = tetromino;
        this.color = color;
    }

    public RotatableTetromino getTetromino()
    {
        return tetromino;
    }

    public void rotateLeft()
    {
        int oldWidth = tetromino.getWidth();
        int oldHight = tetromino.getHeight();

        tetromino.rotateLeft();

        x0 = x0 - (tetromino.getWidth() - oldWidth) / 2;
        y0 = y0 - (tetromino.getHeight() - oldHight) / 2;
    }

    public void rotateRight()
    {
        int oldWidth = tetromino.getWidth();
        int oldHight = tetromino.getHeight();

        tetromino.rotateRight();

        x0 = x0 - (tetromino.getWidth() - oldWidth) / 2;
        y0 = y0 - (tetromino.getHeight() - oldHight) / 2;
    }

    public int getWidth()
    {
        return tetromino.getWidth();
    }

    public int getHeight()
    {
        return tetromino.getHeight();
    }

    public int getX()
    {
        return x0;
    }

    public int getY()
    {
        return y0;
    }

    public Color getColor()
    {
        return color;
    }

    public void moveDown()
    {
        y0++;
    }

    public void moveUp()
    {
        y0--;
    }

    public void moveTo(int x, int y) {
        x0 = x;
        y0 = y;
    }

    public void moveLeft()
    {
        x0--;
    }

    public void moveLeft(int dx)
    {
        x0 -= dx;
    }


    public void moveRight()
    {
        x0++;
    }

    public List<Square> getSquares()
    {
        List<Square> result = new ArrayList<Square>();
        for (int x = 0; x < tetromino.getWidth(); x++)
        {
            for (int y = 0; y < tetromino.getHeight(); y++)
            {
                if (tetromino.isSquare(x, y))
                {
                    result.add(new Square(x0 + x, y0 + y));
                }
            }
        }
        return result;
    }

    public List<Square> getBottomSquares()
    {
        List<Square> result = new ArrayList<Square>();
        for (int x = 0; x < tetromino.getWidth(); x++) {
            for (int y = tetromino.getHeight() - 1; y >= 0 ; y--) {
                if (tetromino.isSquare(x, y)) {
                    result.add(new Square(x0 + x, y0 + y));
                    break;
                }
            }
        }
        return result;
    }

    public List<Square> getLeftSquares()
    {
        List<Square> result = new ArrayList<Square>();
        for (int y = 0; y < tetromino.getHeight(); y++) {
            for (int x = 0; x < tetromino.getWidth(); x++) {
                if (tetromino.isSquare(x, y)) {
                    result.add(new Square(x0 + x, y0 + y));
                    break;
                }
            }
        }
        return result;
    }

    public List<Square> getRightSquares()
    {
        List<Square> result = new ArrayList<Square>();
        for (int y = 0; y < tetromino.getHeight(); y++) {
            for (int x = tetromino.getWidth() - 1; x >= 0 ; x--) {
                if (tetromino.isSquare(x, y)) {
                    result.add(new Square(x0 + x, y0 + y));
                    break;
                }
            }
        }
        return result;
    }

}
